package com.wyy.service.impl;

import com.wyy.bean.Menu;
import com.wyy.bean.Role;
import com.wyy.bean.Users;
import com.wyy.dao.RoleDao;
import com.wyy.dao.UsersDao;
import com.wyy.dao.impl.RoleDaoImpl;
import com.wyy.dao.impl.UsersDaoImpl;
import com.wyy.service.UsersService;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UsersServiceImpl
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/12 14:55
 * @Version 1.0
 **/
public class UsersServiceImpl implements UsersService {
    
    private UsersDao usersDao = new UsersDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();
    @Override
    public Users login(String username, String password) {
        Users user = usersDao.login(username, password);
        if (user == null){
            return null;
        }
        Integer roleId = user.getRoleId();
        Role role = roleDao.findById(roleId);
        List<Menu> menuList = role.getMenuList();
        List<Menu> newMenuList = new ArrayList<>();

        for (Menu menu : menuList) {
            //如果upMenuId==0 遍历其他菜单 找到二级菜单 加入新集合
            if (menu.getUpmenuId() == 0){
                ArrayList<Menu> secondMenuList = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    if (menu1.getUpmenuId() == menu.getMenuId()){
                        secondMenuList.add(menu1);
                    }
                }
                menu.setSecondMenuList(secondMenuList);
                newMenuList.add(menu);
            }
        }
        role.setMenuList(newMenuList);
        user.setRole(role);
        return user;
    }
    
    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        return usersDao.getUsersList(pageIndex,pageSize);
    }
    
    @Override
    public int total() {
        return usersDao.total();
    }
    
    @Override
    public int insertUser(Users users) {
        return usersDao.insertUser(users);
    }
}
